<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>props的简写方式e</title>
  </head>
  <body>
    <div id="test1"></div>
    <div id="test2"></div>
    <div id="test3"></div>
    <!-- 引入react核心库  优先 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <!-- 引入react-dom用于支持react操作DOM，依赖react核心库 -->
    <script
      type="text/javascript"
      src="../js/react-dom.development.js"
    ></script>
    <!-- 引入babel，用于将jsx转换为js -->
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <!-- 引入prop-types，用于对组件标签属性进行限制 -->
    <script type="text/javascript" src="../js/prop-types.js"></script>

    <script type="text/babel">
      class Person extends React.Component {
        //props的简写方式
        //就是把 peopsTypes 和 defaultProps 直接写在类的内部
        // =========================================
        static propTypes = {
          name: PropTypes.string.isRequired, //限制name必须为字符串
          age: PropTypes.number, //限制age必须为数字
          speak: PropTypes.func, //限制speak必须为函数  函数 用func
        };
        //设置默认值
        static defaultProps = {
          age: 100, //默认值
        };
        // ===========================
        render() {
          const { name, age } = this.props;
          //   props是只读的，不允许修改
          return (
            <ul>
              <li>{name}</li>
              <li>{age + 1}</li>
            </ul>
          );
        }
      }

      const p = { name: "lisa", age: 31 };
      ReactDOM.render(
        <Person name={p.name} age={p.age} />,
        document.getElementById("test1")
      );
      //限制props类型
      ReactDOM.render(<Person name="Tom" />, document.getElementById("test2"));
      const p1 = { name: "sunliang", age: 29 };
      ReactDOM.render(
        <Person {...p1} speak={speak} />,
        document.getElementById("test3")
      );
      function speak() {
        console.log("speak,我要说话了");
      }
      //   ReactDOM.render(<Person {...p} />, document.getElementById("test1"));
    </script>
  </body>
</html>
